package cn.rkylin.oms.order.adapter;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

import cn.rkylin.core.IDataBaseFactory;
import cn.rkylin.oms.order.domain.EcOrder;
import cn.rkylin.oms.order.domain.EcTrade;

/**
 * 
 * @author Administrator
 *
 */
@Service("taobaoOrderService")
public class TaobaoOrderService {

    @Resource(name = "redisTemplate")
    private RedisTemplate<Serializable, Serializable> redisTemplate;
    
    @Autowired
    private IDataBaseFactory dao;
    
    /**
     * 
     * @param updateStatement
     * @param ecTrade
     * @return
     * @throws Exception
     */
    public int update(String updateStatement, EcOrder ecOrder) throws Exception {
        ecOrder.setUpdateTime(new Date());
        int result = dao.update(updateStatement, ecOrder);
        //redis 更新
        if (result!=0){
            updateRedis(ecOrder);
        }
        return result;
    }
    
    public int insert(String pageStatement, EcOrder ecOrder) throws Exception {
        Date date = new Date();
        ecOrder.setUpdateTime(date);
        ecOrder.setCreateTime(date);
        int result = dao.insert(pageStatement, ecOrder);
        //redis 更新
        if (result!=0){
            updateRedis(ecOrder);
        }
        return result;
    }
    
    private void updateRedis(EcOrder ecOrder) throws Exception{
        
        List<String> list = null;
        //redis 更新
        ValueOperations<Serializable, Serializable> valueops = redisTemplate.opsForValue();
        list = (ArrayList<String>)valueops.get("淘宝-Order-EcTradeId:"+ecOrder.getEcTradeId());
        if (list!=null && list.size()>0){
            boolean flg = false; 
            for (int i = 0 ; i < list.size() ; i++){
                if (list.get(i).equals(ecOrder.getEcOrderId())){
                    flg = true;
                    break;
                }
            }
            if (!flg){
                list.add(ecOrder.getEcOrderId());
            }
        } else {
            list = new ArrayList<String>();
            list.add(ecOrder.getEcOrderId());
        }
        valueops.set("淘宝-Order-EcTradeId:"+ecOrder.getEcTradeId(), (Serializable)list); 
    }
    
    /**
     * 按照tradeId，商品编码，规格编码检索出来Order数据
     * @param ecTradeId
     * @param ecItemCode
     * @param ecSkuCode
     * @return
     * @throws Exception
     */
    public EcOrder selectByItem(String ecTradeId , String ecItemCode , String ecSkuCode) throws Exception {
        
        //从redis中取得
        ValueOperations<Serializable, Serializable> valueops = redisTemplate.opsForValue();
        List<String> list = (ArrayList<String>)valueops.get("淘宝-Order-EcTradeId:"+ecTradeId);
        EcOrder ecOrder = null;
        boolean flg = false;
        if (list!=null && list.size()>0){
            for (int i = 0 ; i < list.size() ; i++){
                ecOrder = (EcOrder)dao.find("selectByPrimaryKeyEcOrder", list.get(i));
                if (ecOrder.getEcTradeId().equals(ecTradeId) && 
                        ecOrder.getEcItemCode().equals(ecItemCode) &&
                        ((ecSkuCode!=null && ecOrder.getEcSkuCode().equals(ecSkuCode)) || (ecSkuCode==null && ecOrder.getEcSkuCode()== null))){
                    flg = true;
                    break;
                }
            }
        }
        
        //如果redis中没有取到就从数据库中取得
        if (!flg){
            EcOrder param = new EcOrder();
            param.setEcTradeId(ecTradeId);
            param.setEcItemCode(ecItemCode);
            param.setEcSkuCode(ecSkuCode);
            try {
                ecOrder = (EcOrder)dao.find("selectByItem", param);
                if(ecOrder!=null){
                    if (list==null){
                        list = new ArrayList<String>();
                    }
                    list.add(ecOrder.getEcOrderId());
                    valueops.set("淘宝-Order-EcTradeId:"+ecTradeId, (Serializable)list);    
                }
            } catch (Exception e) {
                ecOrder = null;
                e.printStackTrace();
            } 
        }
        
        return ecOrder;
    }
    
    /**
     * 按照tradeId，商品编码，规格编码检索出来Order数据(数据同步)
     * @param ecTradeId
     * @param ecItemCode
     * @param ecSkuCode
     * @return
     * @throws Exception
     */
    public List<EcOrder> selectByTradeId(List<EcTrade> listTrade) throws Exception {
        if (listTrade==null || listTrade.size() <= 0){
            return new ArrayList<EcOrder>();
        }
        
        List<EcOrder> list = new ArrayList<EcOrder>();
        ValueOperations<Serializable, Serializable> valueops = redisTemplate.opsForValue();
        
      //从redis中取得
        for (int i = 0 ; i < listTrade.size() ; i++){
            List<String> listOrder = (ArrayList<String>)valueops.get("淘宝-Order-EcTradeId:"+listTrade.get(i).getEcTradeId());
            if (listOrder==null){
                List<EcOrder> orders = new ArrayList<EcOrder>();
                Map<String, Object> param = new HashMap<String, Object>();
                param.put("ecTradeId", listTrade.get(i).getEcTradeId());
                orders = dao.findForList("selectSplitItem", param);
                if (orders!=null && orders.size()>0){
                    list.addAll(orders);
                    listOrder = new ArrayList<String>();
                    for (int j = 0 ; j < orders.size() ; j++){
                        listOrder.add(orders.get(j).getEcOrderId());
                    }
                    valueops.set("淘宝-Order-EcTradeId:"+listTrade.get(i).getEcTradeId(), (Serializable)listOrder); 
                }
            }
            
            if (listOrder==null){
                continue;
            }
            
            for (int j = 0 ; j < listOrder.size() ; j++){
                list.add((EcOrder)dao.find("selectByPrimaryKeyEcOrder", listOrder.get(j)));
            }
        }
        
//      //如果redis中没有取到就从数据库中取得
//        if (list==null || list.size()<=0){
//            for (int i = 0 ; i < listTrade.size() ; i++){
//                List<EcOrder> orders = new ArrayList<EcOrder>();
//                Map<String, Object> param = new HashMap<String, Object>();
//                param.put("ecTradeId", listTrade.get(i).getEcTradeId());
//                orders = dao.findForList("selectSplitItem", param);
//                list.addAll(orders);
//                List<String> listOrder = new ArrayList<String>();
//                for (int j = 0 ; j < orders.size() ; j++){
//                    listOrder.add(orders.get(j).getEcOrderId());
//                }
//                valueops.set("淘宝-Order-EcTradeId:"+listTrade.get(i).getEcTradeId(), (Serializable)listOrder); 
//            }
//            
//        }
        
        return list;
    }
}
